package cn.ctvonline.android.kaopu.widget;

import android.content.Context;
import android.graphics.drawable.StateListDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.ctvonline.android.kaopu.R;
import cn.ctvonline.android.kaopu.entity.ExpandEntry;

/**
 * Created by dujian on 15/12/2.
 */
public class SimpleChooserItemAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<ExpandEntry> valueList;
    private OnRecyleItemClickListener onRecyleItemClickListener;
    private StateListDrawable itemStateDrawable;

    public SimpleChooserItemAdapter(Context context, List<ExpandEntry> valueList) {
        this.context = context;
        this.valueList = valueList;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final SimpleChooserViewHolder scHolder = (SimpleChooserViewHolder) holder;
        ExpandEntry entry = valueList.get(position);
        scHolder.moreIv.setVisibility(entry.isHasMore() ? View.VISIBLE : View.GONE);
        scHolder.valueTv.setText(entry.getValue());
        if (onRecyleItemClickListener != null) {
            scHolder.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onRecyleItemClickListener.onRecycleItemClick(scHolder.view, position);
                }
            });
        }
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public List<ExpandEntry> getValueList() {
        return valueList;
    }

    public void setValueList(List<ExpandEntry> valueList) {
        this.valueList = valueList;
    }

    public OnRecyleItemClickListener getOnRecyleItemClickListener() {
        return onRecyleItemClickListener;
    }

    @Override
    public int getItemCount() {
        return valueList == null ? 0 : valueList.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_simple_value_item, parent, false);
        if (itemStateDrawable != null) {
            view.setBackground(itemStateDrawable);
        }
        SimpleChooserViewHolder viewHolder = new SimpleChooserViewHolder(view);
        return viewHolder;
    }

    public void setOnRecyleItemClickListener(OnRecyleItemClickListener onRecyleItemClickListener) {
        this.onRecyleItemClickListener = onRecyleItemClickListener;
    }

    static class SimpleChooserViewHolder extends RecyclerView.ViewHolder {
        public View view;
        public ImageView itemIv;
        public TextView valueTv;
        public ImageView moreIv;

        public SimpleChooserViewHolder(View view) {
            super(view);
            this.view = view;
            this.itemIv = (ImageView) view.findViewById(R.id.dialog_simple_item_icon_iv);
            this.valueTv = (TextView) view.findViewById(R.id.dialog_simple_item_tv);
            this.moreIv = (ImageView) view.findViewById(R.id.dialog_simple_item_more_iv);
        }
    }

    public void setItemStateDrawable(StateListDrawable itemStateDrawable) {
        this.itemStateDrawable = itemStateDrawable;
    }

    public interface OnRecyleItemClickListener {
        void onRecycleItemClick(View view, int position);
    }
}
